package com.css.fxfzypg.modules.baseData.repository.impl;

import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.baseData.entity.AnrrsmEntity;
import com.css.fxfzypg.modules.baseData.entity.ErcmEntity;
import com.css.fxfzypg.modules.baseData.queryParams.ErcmQueryParams;
import com.css.fxfzypg.modules.baseData.repository.ErcmRepository;
import com.css.fxfzypg.util.PlatformObjectUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @create 2022-08-29 16:37
 */
@Repository
public class ErcmRepositoryImpl implements ErcmRepository {


    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public ErcmEntity saveErcm(ErcmEntity ercmEntity) {
        String sql = "";
        sql = "insert into data_ercm values(:ercmId, :ercmProvince, :ercmCity, :ercmPrefecture, :ercmRfasi, :ercmEdcai, :ercmTssAer, :ercmAssAer,:ercmPssAer,:ercmTmir,:ercmClimateCr,:ercmNationalCr,:ercmTdwrapo,:ercmOlftdwCr,:ercmCreatetime,:ercmUpdatetime,:ercmIsdelete,:provinceId,:cityId,:prefectureId) ";
        SqlParameterSource source = new BeanPropertySqlParameterSource(ercmEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return ercmEntity;
    }

    @Override
    public Object getErcmById(String id) {
        String sql="";
        sql="select * from data_ercm where ercm_id='"+id+"'";
        ErcmEntity ercmEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(ErcmEntity.class));
        return ercmEntity;
    }

    @Override
    public ErcmEntity updateErcm(ErcmEntity ercmEntity) {
        String sql="";
        sql="update data_ercm  set  ercm_province = '"+ercmEntity.getErcmProvince()+"', ercm_city = '"+ercmEntity.getErcmCity()+"', ercm_prefecture = '"+ercmEntity.getErcmPrefecture()+"', ercm_rfasi = '"+ercmEntity.getErcmRfasi()+"', ercm_edcai = '"+ercmEntity.getErcmEdcai()+"',ercm_tss_aer='"+ercmEntity.getErcmTssAer()+"' ,ercm_ass_aer='"+ercmEntity.getErcmAssAer()+"',ercm_pss_aer='"+ercmEntity.getErcmPssAer()+"',ercm_tmir='"+ercmEntity.getErcmTmir()+"',ercm_climate_cr='"+ercmEntity.getErcmClimateCr()+"',ercm_national_cr='"+ercmEntity.getErcmNationalCr()+"',ercm_tdwrapo='"+ercmEntity.getErcmTdwrapo()+"',ercm_olftdw_cr='"+ercmEntity.getErcmOlftdwCr()+"',ercm_updatetime='"+ercmEntity.getErcmUpdatetime()+"', province_id='"+ercmEntity.getProvinceId()+"' ,city_id ='"+ercmEntity.getCityId()+"', prefecture_id='"+ercmEntity.getPrefectureId()+ "' where ercm_id='"+ercmEntity.getErcmId()+"'";
        jdbcTemplate.update(sql);
        return ercmEntity;
    }

    @Override
    public void deleteErcm(List<String> ids, Timestamp currentTimestamp) {
        String sql = "update data_ercm set ercm_isdelete = '" + YNEnum.N.toString() + "' ," +
                " ercm_updatetime = '"+ currentTimestamp +"' " +
                " where ercm_id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);
    }

    @Override
    public  Map<String,Object> getAllErcmByParams(ErcmQueryParams queryParams) {
        StringBuilder sql = new StringBuilder(" select  u.* from data_ercm u ");

        StringBuilder orderSql = new StringBuilder(" order by u.ercm_createtime");

        StringBuilder whereSql = new StringBuilder(" where u.ercm_isdelete = '" + YNEnum.Y.toString() + "'");

        if (!PlatformObjectUtils.isEmpty(queryParams.getProvinceId())) {
            whereSql.append(" and u.province_id ='"+queryParams.getProvinceId()+"'");
        }

        if (!PlatformObjectUtils.isEmpty(queryParams.getCityId())) {
            whereSql.append(" and u.city_id  ='" + queryParams.getCityId() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getPrefectureId())) {
            whereSql.append(" and u.prefecture_id ='" + queryParams.getPrefectureId() + "'");
        }
        int firstIndex=(queryParams.getCurPage()-1) * queryParams.getPageSize();
        int lastIndex=queryParams.getCurPage() * queryParams.getPageSize();
        sql.append(whereSql).append(orderSql).toString();
        StringBuilder limitSql = new StringBuilder(" limit "+lastIndex+" offset "+ firstIndex);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(AnrrsmEntity.class)).size();
        List<ErcmEntity> ercmEntities = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(ErcmEntity.class));
        Map<String,Object> map=new HashMap();
        map.put("total",total);
        map.put("rows",ercmEntities);
        return map;
    }
}
